package com.dd.cloud.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarUtil {

    /**
     * 计算两个日期之间的差值，单位为分钟
     *
     * @param expireDate
     * @param now
     * @return
     */
    public static long timeDiff(Date expireDate, Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(expireDate);
        long expireTime = calendar.getTimeInMillis();
        calendar.setTime(now);
        long nowTime = calendar.getTimeInMillis();
        long dx = ((expireTime - nowTime) / 1000 / 60);
        return dx;
    }

    public static Integer getAge(String birth) {
        Date date = DateUtils.getDate_ymd(birth);
        Calendar calendar = Calendar.getInstance();
        if (calendar.before(date)) {
            System.out.println("================出生日期大于当前日期===============");
            return -1;
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.setTime(date);
        int years = calendar.get(Calendar.YEAR);
        int months = calendar.get(Calendar.MONTH) + 1;
        int days = calendar.get(Calendar.DAY_OF_MONTH);

        int age = year - years;

        if (month <= months) {
            if (month == months) {
                if (day < days) {
                    age--;
                }
            }
        }
        return age;
    }

    /**
     * @param birth:
     * @return Integer
     * @author czg
     * @description 出生年月获取年龄
     * @date 2022/11/30 16:17
     */
    public static Integer getAge(LocalDate birth) {
        if (birth == null) {
            return null;
        }
        LocalDate date1 = LocalDate.now();
        return birth.until(date1).getYears();
    }

    /**
     * 获取
     * 本日时段 [10:00, 12:00, 14:00, 16:00, 18:00, 20:00]
     * 本周时段 [6-1, 6-2, 6-3, 6-4, 6-5, 6-6, 6-7]
     * 本月时段 [6-1, 6-2, 6-3, 6-4, 6-5, 6-6, 6-7, 6-8, 6-9, 6-10, 6-11, 6-12, 6-13, 6-14, 6-15, 6-16, 6-17, 6-18, 6-19, 6-20, 6-21, 6-22, 6-23, 6-24, 6-25, 6-26, 6-27, 6-28, 6-29, 6-30]
     * 本年季度时段 [1, 2, 3, 4]
     * 本年时段 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
     *
     * @param value 1本日时段 2本周时段 3本月时段 4本季度时段 5本年时段
     * @param split 分隔符
     * @return
     */
    public static List<String> getTimeSlot(Integer value, String split) {
        if (StringUtils.isBlank(split)) {
            split = "-";
        }
        List<String> list = new ArrayList<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(new Date());
        switch (value) {
            case 1:
                list.add("10:00");
                list.add("12:00");
                list.add("14:00");
                list.add("16:00");
                list.add("18:00");
                list.add("20:00");
                break;
            case 2:
                calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                for (int i = 1; i <= 7; i++) {
                    list.add(calendar1.get(Calendar.MONTH) + 1 + split + calendar1.get(Calendar.DATE));
                    calendar1.add(Calendar.DATE, 1);
                }
                break;
            case 3:
                calendar1.set(Calendar.DAY_OF_MONTH, 1);
                calendar1.add(Calendar.MONTH, 0);
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(new Date());
                calendar2.set(Calendar.DAY_OF_MONTH, 0);
                calendar2.add(Calendar.MONTH, 1);
                for (int i = calendar1.get(Calendar.DATE); i <= calendar2.get(Calendar.DATE); i++) {
                    list.add(calendar1.get(Calendar.MONTH) + 1 + split + calendar1.get(Calendar.DATE));
                    calendar1.add(Calendar.DATE, 1);
                }
                break;
            case 4:
                list.add("1");
                list.add("2");
                list.add("3");
                list.add("4");
                break;
            case 5:
                list.add("1");
                list.add("2");
                list.add("3");
                list.add("4");
                list.add("5");
                list.add("6");
                list.add("7");
                list.add("8");
                list.add("9");
                list.add("10");
                list.add("11");
                list.add("12");
            default:
        }
        return list;
    }

    /**
     * 两时间差
     * == 0 分时
     * <= 31 天
     * <= 366 月
     * >366 年
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getTimeSlot(LocalDateTime start, LocalDateTime end) {
        List<String> list = new ArrayList<>();
        long day = Duration.between(start, end).toDays();
        if (day == 0) {
            list.add("10:00");
            list.add("12:00");
            list.add("14:00");
            list.add("16:00");
            list.add("18:00");
            list.add("20:00");
        } else if (day <= 31) {
            if (start.getMonthValue() != end.getMonthValue()) {
                int lastDay = start.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
                for (int i = start.getDayOfMonth(); i <= lastDay; i++) {
                    list.add(start.getMonthValue() + "-" + i);
                }
                for (int i = 1; i <= end.getDayOfMonth(); i++) {
                    list.add(end.getMonthValue() + "-" + i);
                }
            } else {
                for (int i = start.getDayOfMonth(); i <= end.getDayOfMonth(); i++) {
                    list.add(start.getMonthValue() + "-" + i);
                }
            }
        } else if (day <= 366) {
            if (start.getYear() < end.getYear()) {
                for (int i = start.getMonthValue(); i <= 12; i++) {
                    list.add(start.getYear() + "-" + i);
                }
                for (int i = 1; i <= end.getMonthValue(); i++) {
                    list.add(end.getYear() + "-" + i);
                }
            } else {
                for (int i = start.getMonthValue(); i <= end.getMonthValue(); i++) {
                    list.add(start.getYear() + "-" + i);
                }
            }
        } else {
            for (int i = start.getYear(); i <= end.getYear(); i++) {
                list.add(i + "");
            }
        }
        return list;
    }

    /**
     * 计算两时间的 天时分秒
     *
     * @param expireDate
     * @param now
     * @return
     */
    public static String calcTime(Date expireDate, Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(expireDate);//截止时间
        long expireTime = calendar.getTimeInMillis();
        calendar.setTime(now);//当前时间
        long nowTime = calendar.getTimeInMillis();
        long td = expireTime - nowTime; //时差

//        long calc = 0;
//        long day = td / (24 * 60 * 60 * 1000); //天数
//        long hour = 0; //小时数
//        long minute = 0; //分钟数
//        long sec = 0; //秒数
//        StringBuilder sb = new StringBuilder();
//        if (day > 0) {
//            sb.append(day + "天");
//        }
//        if (td % (24 * 60 * 60 * 1000) != 0) {
//            calc = td - (day * 24 * 60 * 60 * 1000);
//            hour = calc / (60 * 60 * 1000);
//            sb.append(" " + hour);
//        }
//        if (td % (60 * 60 * 1000) != 0) {
//            calc = td - (day * 24 * 60 * 60 * 1000) - (hour * 60 * 60 * 1000);
//            minute = calc / (60 * 1000);
//            sb.append(":" + minute);
//        }
//        if (td % (60 * 1000) != 0) {
//            calc = td - (day * 24 * 60 * 60 * 1000) - (hour * 60 * 60 * 1000) - (minute * 60 * 1000);
//            sec = calc / 1000;
//            sb.append(":" + sec);
//        }
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        long day = td / dd; //天数
        long hour = (td - day * dd) / hh; //小时数
        long minute = (td - day * dd - hour * hh) / mi; //分钟数
        long sec = (td - day * dd - hour * hh - minute * mi) / ss; //秒数
        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(" " + hour);
        }
        if (minute > 0) {
            sb.append(":" + minute);
        }
        if (sec > 0) {
            sb.append(":" + sec);
        }

        return sb.toString();
    }
}
